package com.library.common.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.library.common.R


/***
 *
 */
abstract class MBaseRecycAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val mData = ArrayList<T?>(1)
    protected var mView: RecyclerView? = null
    protected var mContext: Context? = null
    protected var listener: ((v: View, position: Int) -> Unit)? = null
    protected var HEARDER_VIEW = 1//头布局
    protected var FOOT_VIEW = 2//脚布局
    protected var NORMAL_VIEW = 3//普通布局
    protected var normalView: Int? = null
    protected var headView: Int? = null
    protected var footView: Int? = null

    protected open fun normalViewId(): Int = 0
    protected open fun headViewId(): Int = 0
    protected open fun footViewId(): Int = R.layout.foot_view


    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (mContext == null)
            mContext = viewGroup.context
        return when (viewType) {
            HEARDER_VIEW -> ViewHolder(
                LayoutInflater.from(mContext).inflate(headViewId(), viewGroup, false)
            )
            FOOT_VIEW -> ViewHolder(
                LayoutInflater.from(mContext).inflate(footViewId(), viewGroup, false)
            )
            else -> ViewHolder(
                LayoutInflater.from(mContext).inflate(normalViewId(), viewGroup, false)
            )
        }
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) != FOOT_VIEW || getItemViewType(position) != HEARDER_VIEW) {
            onHolder(viewHolder as ViewHolder, if (headView != null) viewHolder.layoutPosition + 1 else viewHolder.layoutPosition)
        }
        //为啥使用notifyItemInserted之后position不会加1，BUG？
        //所以下面用viewHolder.layoutPosition，确保position正确
        if (listener != null)
            viewHolder.itemView.setOnClickListener { v ->
                if (getItemViewType(position) != FOOT_VIEW) {//底部布局，点击没效果
                    if (getItemViewType(position) != HEARDER_VIEW) {//头布局点击没效果
                        listener!!.invoke(v, viewHolder.layoutPosition)
                    }
                }
            }
    }

    protected abstract fun onHolder(holder: ViewHolder, position: Int)


    override fun getItemCount(): Int {
        return mData.size
    }

    fun bindRecyclerView(mView: View?) {
        this.mView = mView as? RecyclerView
    }

    fun setNewData(data: MutableList<T>?) {
        mData.clear()
        if (data != null)
            mData.addAll(data)
        notifyDataSetChanged()
    }

    fun addData(data: MutableList<T>?) {
        addData(data, mData.size)
    }

    fun addData(data: MutableList<T>?, position: Int) {
        if (data == null)
            return
        mData.addAll(data)
        notifyItemInserted(position)
    }

    fun addData(vararg data: T?) {
        addData(mData.size, *data)
    }

    fun addData(position: Int, vararg data: T?) {
        mData.addAll(position, data.toList())
        notifyItemInserted(position)
    }

    fun remove(t: T?) {
        val indexOf = mData.indexOf(t)
        if (indexOf != -1)
            removeAt(indexOf)
    }

    fun removeAt(position: Int) {
        if (position < mData.size)
            mData.removeAt(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, mData.size - position)
    }

    fun setOnItemClickListener(listener: ((v: View, position: Int) -> Unit)) {
        this.listener = listener
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun <T : View> getView(id: Int): T? {
            return itemView.findViewById(id)
        }
    }
}
